package org.whp.mealplanner.server;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InvalidClassException;
import java.io.NotSerializableException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.IdentityType;


@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Ingredient implements Serializable {
	
	transient ByteArrayOutputStream bos;
	transient ObjectOutputStream oos;
	transient byte[] objectsBytes;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -8769318924303734884L;

	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    private String encodedKey;
	
	@Persistent
	private String name;
	
	@Persistent
	private String units;
	
	public Ingredient(String name, String units) {
		this.name = name;
		this.units = units;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUnits() {
		return units;
	}

	public void setUnits(String units) {
		this.units = units;
	}

	public String getEncodedKey() {
		return encodedKey;
	}
	
	public byte[] serialize() {
		bos = new ByteArrayOutputStream();
		try {
			oos = new ObjectOutputStream(bos);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
			try {
				oos.writeObject(this);
			} catch (InvalidClassException e) {
				e.printStackTrace();
			} catch (NotSerializableException e1) {
				e1.printStackTrace();
			} catch (IOException e2) {
				e2.printStackTrace();
			}
			objectsBytes = bos.toByteArray();
		
			return objectsBytes;
	}
	

}
